
/* "My Requiem: (imho) The most serious development I am involved with"
 *  By: R. J. Cano, On Monday Aug 25 2015 04:20 GMT
*/

combsum(A,m)={my(ans,y,t);ans=0;forvec(x=vector(m,j,[1,#A]),for(j=1,m!,y=numtoperm(m,j-1);t=1;for(l=1,#y,t*=A[x[y[l]]]);ans+=t),2);ans}

/* Example 1: Copy and paste in gp the content of following comment as is, without the "Begin"/"end" delimiter lines. */

/* Begin * /

\p 111
combsum(A,m)={my(ans,y,t);ans=0;forvec(x=vector(m,j,[1,#A]),for(j=1,m,y=numtoperm(m,j-1);t=1;for(l=1,#y,t*=A[x[y[l]]]);ans+=t),2);ans}
w=vector(random()%22+2,j,random()/(random()+1)+.);
sum(k=1,#w,w[k]^2)+combsum(w,2)-(vecsum(w))^2

/ * End */

/* Example 2: Copy and paste in gp the content of following comment as is, without the "Begin"/"end" delimiter lines. */

/* Begin * /

\p 111 
combsum(A,m)={my(ans,y,t);ans=0;forvec(x=vector(m,j,[1,#A]),for(j=1,m!,y=numtoperm(m,j-1);t=1;for(l=1,#y,t*=A[x[y[l]]]);ans+=t),2);ans}
f(A,m)={sum(k=1,#A,A[k]^m)+sum(i=2,m,stirling(m,i,2)*combsum(A,i))}
my(q);
w=vector(random()%22+2,j,1);
for(u=2,4,q=vecsum(w)^u - f(w,u);print(u" "q));
\\ Again, but now the more general case applied to arbitrary complex numbers.
print("Failing for now...")
w=vector(random()%22+2,j,random()/(random()+1)+random()/(random()+1)*I+.);
for(u=2,4,q=vecsum(w)^u - f(w,u);print(u" "q));

/ * End */

/*
 * Explanation: Additional structure is missing at the end in the
 * example 2. Only when every component in A is set to 1, such
 * structure can be ignored. Indeed, such structure necessarily
 * must generate the so called "Stirling numbers of the second kind".
*/

/* Brief history:
 * 
 * The author spent 5 years obsessed with this, since Dec 27th 2010,
 * and he found himself in love with the arithmetic, combinatorics,
 * and all the Mathematics. He was able to re-discover the so called
 * Stirling number of the second kind (2011), and some other nice
 * realizations, mainly already well known and useful in several fields
 * of the mathematics, some of them closely related to number theory,
 * and the others consisting in algorithms and principles.
 * 
 * Today he knows a curious algorithm alternative to the multinomial
 * theorem (such one due to Bernoulli and Leibniz), about computing these
 * kind of powers. But more yet, beyond that, a nice way of getting
 * the Stirling numbers of the second kind, the Bell numbers and
 * very precise/detailed algebraic expressions useful for this kind
 * of computations.
 * 
 * The initial inspiration came from the physics, where the author
 * ignored for long time how to deal efficiently with the Taylor's
 * theorem applied in small oscillations (for example), being
 * already aware and distressed by the need of speeding up and
 * optimization for those calculations, perhaps such attitude was
 * initially "wrong" (in the sense of being unnecessary), but
 * it was unknown for the author of this source code the fact that
 * such quest will drive him directly to the Stirling numbers & more!!!. 
*/  

/**
 * 

 On the "missing structure". It's algorithm and introduction:
 == === ==================== ==== ========= === =============
 
 People who is familiar with applied calculus in Physics usually
 learn Tensor Calculus for General Relativity.
 
 One of the elementary tools taught there is something called "delta",
 or "Kronecker's Delta", a rule of comparison between two integers
 called indices such that it assigns a binary digit to every pair
 of positive integers, and has associated the identity matrix as
 representation since it is often referred as a symmetric tensor by
 definition.
 
 Now, by thinking in matrices, we might consider this simple question:
 "Where are the other 1s?, those that would complete the identity matrix
 in order to have an square matrix with 1s everywhere".
 
 Well, another way to state it, could be: Let us define the NOT operation
 for matrices by assigning to a matrix, another where it is applied the NOT
 operator to each component: Given "z" any integer number, NOT(z) is equals
 to 1 if and only if z is zero, otherwise NOT(z) is zero.
 
 Based upon the preceding definition, the question now turns into:
 What is: "NOT(zeroMatrix(n*n))-identityMatrix(n)" ????
 
 Of course this is a problem of (irreducible) representations...
 
 (1) The NOT(ZeroMatrix(n*n)) is symmetric.
 (2) The identityMatrix(n) is symmetric.
 
 Therefore: When we subtract (2) from (1), the result must be symmetric.
 
 (3) We are dealing with square matrices.
 (4) Square matrices have two indices.
 
 Therefore: Any symmetric matrix "S" could be viewed as the sum of three parts,
            three other matrices:
            
             a) "s0" A diagonal matrix (the identity matrix for example).
             b) "s1" An strict (upper or lower) triangular matrix.
             c) "s2" The transpose of "s1".

 All those three parts are here easily identified for (1).     
  
 Ok, And: What is actually transposition?...

 Transpositions are essentially commutations of indices,

 And commutations are particular cases of PERMUTATIONS.

 Previously at the examples section, the reason of every
 calculation approx. matching when m=2 is that none additional
 structure is required more than the mentioned PERMUTATIONS if
 we are performing the calculation of the square of a sum or
 the product of two sums with the same quantity of terms.
 
 It is clear if we notice that the unique Stirling number of
 the second kind corresponding there is 1.
 
 And then, all the other cases consisting in highers powers match
 only if the terms in those sums are all the unit (the real unit, only).
 
 But those cases where we are assuming it is required to insert the Stirling
 numbers of the second kind as factors, mismatch for any other different set
 of complex numbers used as the components or terms inside the sums...

 The logical consequence is: The missing structure induces the set of integer
                             numbers known as the "Stirling numbers of the second
                             kind". Consequently also the so called "Bell numbers".
 
           Yes. It is nothing kidding above in the preceding statement,
           It exist at least an algorithm generating a particular recursive
           structure such that it can be used to build a precise expression
           for the product of two or more sums of things labeled with indices
           (of course regardless if they have in common the same number of terms,
           since we always -tricky- could fill empty places with zeros), consistent
           with the "multinomial theorem" when those sums are identical (then becoming
           the integer power of a sum), and such that if we replace each term in all
           the sums with the unit, what we get among other things, are the following
           4 facts:
           
           i) Knowledge about the structure of some columns from the table of Stirling
              numbers of the second kind:
           
           i.1) The second column are always Mersenne numbers: for k>1, Stirling2ndkind(k,2)=2^(k-1)-1;
           i.2) The k-1 column is always the sum of the first integers: for k>1, Stirling2ndkind(k,k-1)=(k-1)*k/2;

           ii) A curious equation: From i.1 and i.2, consider: Stirling2ndkind(k,2)==Stirling2ndkind(k,k-1),

               Which can be reduced to: 2^k-k^2+k-2 == 0

               Which has as solutions k=1, k=2 or k=3 only. And their maxima an minima are given in terms
               of the real branch of Lambert's "W" function.

               ( Comment: This equation inspired the author to propose the sequence A245014 at OEIS.org )
               
           iii) A theorem that surely no few authors could state it is consequence of some definitions:
                (And not a theorem by itself lonely)
                
                N^m = sum{y=1..max(N,m)}( Stirling2ndkind(m,y))
           
              Sample source code about "the theorem" (here restricted to integers) for testing purposes.
           
              (PARI) pow(N,m)=if(m<0,0,if(!m,1,sum(y=1,m,stirling(m,y,2)*binomial(N,y)*(y!))));

              By ignoring the existence of such algorithm, the same which is claimed here in this draft,
              the reader could conclude it is not actually a theorem. But this is actually consequence
              of something more general (such bigger thing is the true theorem), indeed coming from the
              algebra for complex numbers after some convenient simplifications are made: Those
              already mentioned and shown previously about replacing arbitrary real/complex numbers
              with the real unit.
              
              To be continued...
*/